import 'package:flutter/material.dart';

import 'package:junyeyishu/utils/utils.dart';
import 'package:junyeyishu/widgets/QGButton.dart';
import 'package:pin_code_fields/pin_code_fields.dart';

class QGPasswordAlert extends StatefulWidget {
  String title;
  String content;
  String cancel;
  String confirm;
  void Function()? onCancel;
  void Function(String pwd)? onConfirm;
  BuildContext context;

  QGPasswordAlert({
    super.key,
    required this.context,
    required this.title,
    required this.content,
    required this.cancel,
    required this.confirm,
    this.onCancel,
    this.onConfirm,
  });

  static show(context,
      {title = '交易密码',
      content = "",
      cancel = "取消",
      confirm = "确定",
      onCancel,
      onConfirm}) {
    showDialog(
        context: context,
        builder: (context) {
          return QGPasswordAlert(
              context: context,
              title: title,
              content: content,
              cancel: cancel,
              confirm: confirm,
              onCancel: onCancel,
              onConfirm: onConfirm);
        });
  }

  @override
  State<QGPasswordAlert> createState() => _QGPasswordAlertState();
}

class _QGPasswordAlertState extends State<QGPasswordAlert> {
  var controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Container(
          margin: EdgeInsets.symmetric(horizontal: 25.rpx),
          padding: EdgeInsets.symmetric(horizontal: 20.rpx, vertical: 25.rpx),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(20.rpx),
            color: Color(0xFF232A32),
          ),
          child: Column(mainAxisSize: MainAxisSize.min, children: [
            buildHeader(),
            SizedBox(height: 25.rpx),
            buildButtons(),
          ]),
        ),
      ],
    );
  }

  buildHeader() {
    return Column(
      children: [
        Text(
          widget.title,
          style: TextStyle(
              color: Color(0xFFffffff),
              fontSize: 16.rpx,
              fontWeight: FontWeight.bold),
        ),
        SizedBox(height: 25.rpx),
        buildInput(),
        SizedBox(height: widget.content.isEmpty ? 0 : 20.rpx),
        Text(
          widget.content,
          style: TextStyle(
              color: Color(0xFFffffff),
              fontSize: 14.rpx,
              fontWeight: FontWeight.normal),
        ),
      ],
    );
  }

  buildInput() {
    var bgColor = Color(0xFF0C1012);
    return PinCodeTextField(
      keyboardType: TextInputType.number,
      errorTextSpace: 0,
      cursorColor: Color(0xFFffffff),
      cursorHeight: 20.rpx,
      appContext: context,
      length: 6,
      obscureText: true,
      textStyle: TextStyle(fontSize: 10.rpx, color: Color(0xFFffffff)),
      animationType: AnimationType.fade,
      pinTheme: PinTheme(
        shape: PinCodeFieldShape.box,
        borderRadius: BorderRadius.circular(6.rpx),
        fieldHeight: 40.rpx,
        fieldWidth: 40.rpx,
        borderWidth: 0.5.rpx,
        activeColor: bgColor,
        inactiveColor: bgColor,
        selectedColor: bgColor,
        activeFillColor: bgColor,
        inactiveFillColor: bgColor,
        selectedFillColor: bgColor,
      ),
      animationDuration: const Duration(milliseconds: 300),
      enableActiveFill: true,
      controller: controller,
      onCompleted: (value) {},
      onChanged: (value) {},
      beforeTextPaste: (text) => true,
      scrollPadding: EdgeInsets.zero,
    );
  }

  buildButtons() {
    return SizedBox(
      height: 120.rpx,
      child: Column(
        children: [
 QGButton(
                  title: widget.confirm,
                  enable: true,
                  onTap: () {
                    if (widget.onConfirm != null) {
                      widget.onConfirm!(controller.text);
                    } else {
                      Navigator.of(widget.context).pop();
                    }
                  }),
          SizedBox(height: 15.rpx),

          QGOutlineButton(
                  title: widget.cancel,
                  enable: true,
                  onTap: () {
                    if (widget.onCancel != null) {
                      widget.onCancel!();
                    } else {
                      Navigator.of(widget.context).pop();
                    }
                  })

          // Expanded(
          //     child: QGOutlineButton(
          //         title: widget.cancel,
          //         enable: true,
          //         onTap: () {
          //           if (widget.onCancel != null) {
          //             widget.onCancel!();
          //           } else {
          //             Navigator.of(widget.context).pop();
          //           }
          //         })),
          // SizedBox(width: 15.rpx),
          // Expanded(
          //     child: QGButton(
          //         title: widget.confirm,
          //         enable: true,
          //         onTap: () {
          //           if (widget.onConfirm != null) {
          //             widget.onConfirm!(controller.text);
          //           } else {
          //             Navigator.of(widget.context).pop();
          //           }
          //         })),
        ],
      ),
    );
  }
}
